﻿using Business.Service;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcAppWeb.Controllers
{
    public class LocationInventoryBatchController : BaseDataController
    {
        //
        // GET: /LocationInfo/

        public ActionResult LocationInventoryBatch()
        {
            if (Session["UserInfo"] == null) return RedirectToAction("LogOn", "Account");
            else
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                return View(model);

            }
        }


        public JsonResult InitJsonResult(string goodsCode, string goodsName, string cellNo, string saleDays)
        {
            int page = int.Parse(Request["page"].ToString());
            int rows = int.Parse(Request["rows"].ToString());
            int total = 0;
            string where = "  WHERE LI.STATUS <> 0  ";

            if (!string.IsNullOrEmpty(goodsCode))
            {
                where += " and g.goodsCode like'%" + goodsCode + "%'";
            }
            if (!string.IsNullOrEmpty(goodsName))
            {
                where += " and g.goodsName like'%" + goodsName + "%'";
            }
            if (!string.IsNullOrEmpty(cellNo) && cellNo != "无")
            {
                where += " and li.cellNo ='" + cellNo + "'";
            }
            if (!string.IsNullOrEmpty(saleDays) && saleDays == "1")
            {
                where += " and li.saleDays >0";
            }
            else
            {
                where += " AND LI.QUALITYSTATUS=1";
            }
            var retData = new object();
            BLocationInfo li = new BLocationInfo();
            retData = li.GetLocationInventoryBatchList(page, rows, out total, where).ToList();
            Dictionary<string, object> json = new Dictionary<string, object>();
            json.Add("total", total);
            json.Add("rows", retData);
            return Json(json);
        }



        /// <summary>
        /// 修改or新增
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string SaveData()
        {
            string sjson = string.Empty;
            try
            {
                AppUserList model = (AppUserList)Session["UserInfo"];
                ActionType at = new ActionType();

                int locationInventoryId = Convert.ToInt32(Request["hiddenInventoryId"]);
                int saleDays = Convert.ToInt32(Request["txtNumber"]);

                BLocationInfo db = new BLocationInfo();
                int n = db.UpdateLocationInventoryQualityStatus(locationInventoryId, saleDays);
                BAction action = new BAction();
                ActionInfo ai = new ActionInfo();
                ai.ACTIONTYPECODE = 5;
                ai.OPERATION = "修改";
                ai.OPCONTENT = "解锁ID" + locationInventoryId.ToString() + ",解锁天数" + saleDays.ToString();
                ai.OPERATIONOLD = "";
                ai.OPERATIONNEW = "";
                ai.USERCODE = model.USERCODE;
                action.AddLmActionInfo(ai);
                if (n > 0)
                {
                    sjson = "{success:true}";
                }
                else
                {
                    sjson = "{success:false,msg:'添加失败.请联系管理员！'}";
                }
                return sjson;


            }
            catch (Exception ex)
            {
                sjson = "{success:false,msg:'" + ex.Message + "'}";
            }
            return sjson;
        }

    }
}
